<template>
	<div>
    <div>
      <el-row>
        <el-col :offset="2" :span="4">
          <el-input v-model="studentname" placeholder="请输入姓名搜索"></el-input>
        </el-col>
        <el-col :offset="1" :span="6">
          <el-button type="primary" @click="selectbyname(studentname)">搜索</el-button>
        </el-col>
        <el-col :offset="3" :span="6">
          <el-button
          
          @click="handleAdd()" type="primary">新增</el-button>
        </el-col>
        <!-- <el-col :span="6"><div class="grid-content bg-purple-light">4</div></el-col> -->
      </el-row>
    </div>

    <el-col>
      &nbsp;
    </el-col>

		<el-table
    :data="studentData"
    style="width: 100%">
    <el-table-column
      label="姓名"
      width="180">
      <template slot-scope="scope">
        <!-- <i class="el-icon-time"></i> -->
        <span style="margin-left: 10px">{{ scope.row.name }}</span>
      </template>
    </el-table-column>
	<el-table-column
      label="电话号码"
      width="180">
      <template slot-scope="scope">
        <span style="margin-left: 10px">{{ scope.row.phone }}</span>
      </template>
    </el-table-column>
	<el-table-column
      label="专业"
      width="180">
      <template slot-scope="scope">
        <span style="margin-left: 10px">{{ scope.row.major }}</span>
      </template>
    </el-table-column>
    <!-- <el-table-column
      label="姓名"
      width="180">
      <template slot-scope="scope">
        <el-popover trigger="hover" placement="top">
          <p>姓名: {{ scope.row.name }}</p>
          <p>住址: {{ scope.row.phone }}</p>
          <div slot="reference" class="name-wrapper">
            <el-tag size="medium">{{ scope.row.name }}</el-tag>
          </div>
        </el-popover>
      </template>
    </el-table-column> -->


    <el-table-column label="操作">
      <template slot-scope="scope">
        <el-button
          size="mini"
          @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
        <el-button
          size="mini"
          type="danger"
          @click="handleDelete(scope.$index, scope.row)">删除</el-button>
      </template>
    </el-table-column>
  </el-table>
        <el-dialog
          :title=" formdata.studentid== null ? '新增学生' : '更改学生'"
          :visible.sync="dialogVisible"
          width="30%"
          >

          <!-- 数据表单 -->
          <el-form :model="formdata"  top="1vh">
              <el-form-item label="用户名" prop="title">
                <el-input
                  v-model="formdata.name"
                  autocomplete="off"
                ></el-input>
              </el-form-item>
              <el-form-item label="电话" prop="title">
                <el-input
                  v-model="formdata.phone"
                  autocomplete="off"
                ></el-input>
              </el-form-item>
              <el-form-item label="专业" prop="title">
                <el-input
                  v-model="formdata.major"
                  autocomplete="off"
                ></el-input>
              </el-form-item>
          </el-form>
          <span slot="footer" class="dialog-footer" >
            <el-button type="primary" @click="modifyStudent(formdata)" >确 定</el-button>
            <el-button @click="dialogVisible = false" >取 消</el-button>
          </span>
      </el-dialog>
	</div>
</template>



<script>
	import axios from 'axios';
  export default {
    data() {
      return {
        studentname:"",
		    studentData:[],
        formdata:{},
        dialogVisible:false,
      }
    },
    methods: {

      modifyStudent(formdata){
        if(formdata.studentid== null){
              axios.post("http://162.14.107.118:8080/student/insert",formdata
                ).then(res=>{
                  console.log(res.data);
                  this.getStudentInfo();
                })

            }else{
            axios.post("http://162.14.107.118:8080/student/modify",formdata).then(res=>{
              console.log(res.data);
              this.getStudentInfo();
            })
            }
            this.dialogVisible = false
      },
      handleAdd(){
        this.dialogVisible = true
        this.formdata={}
      },
      //根据学生姓名搜索
      selectbyname(studentname){
        axios.get("http://162.14.107.118:8080/student/selectByName/"+studentname).then(res=>{
          this.studentData=res.data.data;
        })
      },
		handleEdit(index, row) {
          this.dialogVisible = true
          this.formdata=row
        	console.log(index, row);
      	},
		handleDelete(index, row) {

        	// console.log(index, row);
          axios.get("http://162.14.107.118:8080/student/delete/"+row.studentid).then(res=>{
            console.log(res.data);
            
            if(res.data.status){
              this.$message({
                message: res.data.msg,
                type: 'success'
              });
            }else{
              this.$message.error(res.data.msg);
            }
            this.getStudentInfo();
          })
      	},
		//获得所有学生信息   
		getStudentInfo(){
			axios.get("http://162.14.107.118:8080/student/getAllStudent").then(res=>{
				this.studentData=res.data.data;
				console.log(this.studentData);
			})
		}
    },
	created(){
		this.getStudentInfo();
	},

  }
</script>
